package com.mdd.usercenter.job;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.usercenter.model.domain.User;
import com.mdd.usercenter.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 缓存预热业务
 */
@Component
@Slf4j
public class PreCacheJob {

    @Autowired
    private UserService userService;

    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    @Autowired
    private RedissonClient redissonClient;

    private List<Long> ids = Arrays.asList(1L,2L);


    /**
     * 每天都对活跃用户进行推荐预热
     */
    @Scheduled(cron = "0 31 14 * * ? ")
    public void  doCacheRecommendUser() {
        //获取分布式锁
        RLock lock = redissonClient.getLock("hb:precachejob:docache:lock");
        try {
            //尝试获取锁
            boolean b = lock.tryLock(0,-1,TimeUnit.MILLISECONDS);
            //拿到锁执行
            if (b) {
                ValueOperations<String, Object> valueOperations = redisTemplate.opsForValue();
                for (Long id : ids) {
                    //从缓存中获取数据
                    String key = "hb:user:recommend:"+id;
                    //分页查询
                    Page<User> userPage1 = new Page<>(1,20);
                    Page<User> page = userService.page(userPage1);
                    try {
                        valueOperations.set(key,page,60, TimeUnit.SECONDS);
                    } catch (Exception e) {
                        log.info("redis写入失败");
                    }
                }
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            //判断是否为当前线程的锁
            if (lock.isHeldByCurrentThread()) {
                //释放锁
                lock.unlock();
            }
        }
    }

    /**
     * 对游客进行缓存
     */
    @Scheduled(cron = "0 31 14 * * ? ")
    public void  doVisitorCacheRecommendUser() {
        //获取分布式锁
        RLock lock = redissonClient.getLock("hb:precachejob:docache:lock");
        try {
            //尝试获取锁
            boolean b = lock.tryLock(0,-1,TimeUnit.MILLISECONDS);
            //拿到锁执行
            if (b) {
                ValueOperations<String, Object> valueOperations = redisTemplate.opsForValue();
                //从缓存中获取数据
                String key = "hb:user:recommend:visitor";
                //分页查询
                Page<User> userPage1 = new Page<>(1,20);
                Page<User> page = userService.page(userPage1);
                try {
                    valueOperations.set(key,page,1, TimeUnit.HOURS);
                } catch (Exception e) {
                    log.info("redis写入失败");
                }
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            //判断是否为当前线程的锁
            if (lock.isHeldByCurrentThread()) {
                //释放锁
                lock.unlock();
            }
        }
    }
}
